package com.qy139.guest.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qy139.entity.CustomerInfo;
import com.qy139.guest.service.ICustomerInfoService;
import com.qy139.util.Result;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Date;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yuhaochen
 * @since 2021-12-01
 */
@RestController
@RequestMapping("/customer-info")
public class CustomerInfoController {
    @Resource
    private ICustomerInfoService customerInfoService;
//查询全部客户信息
//Page分页
//QueryMapper进行模糊查询
    @GetMapping("/findAllGuest")
    public Result findAllCus(Page page, CustomerInfo customerInfo){
        QueryWrapper<CustomerInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("c_name",customerInfo.getCName());
        Page page1 = customerInfoService.page(page,queryWrapper);
        System.out.println(page1);
        return new Result(page1);
    }
//普通查询客户信息
    @GetMapping("/find")
    public Result find(){
        return new Result(customerInfoService.list());
    }

//添加客户信息
    @PostMapping("/addGuest")
    public Result addGuest(@RequestBody CustomerInfo customerInfo){
        return new Result(customerInfoService.addGuest(customerInfo));
    }
//修改客户信息及逻辑删除
    @PutMapping("/updGuest")
    public Result updGuest(@RequestBody CustomerInfo customerInfo){
        return new Result(customerInfoService.updOrUpdStatus(customerInfo));
    }
//物理删除
    /*@DeleteMapping("/del/{id}")
    public Result deleteCus(@PathVariable("id") Integer id){
        return new Result(customerInfoService.removeById(id));
    }*/
    /*用户头像上传阿里云与下载*/
   /* // yourEndpoint填写Bucket所在地域对应的Endpoint。以华东1（杭州）为例，Endpoint填写为https://oss-cn-hangzhou.aliyuncs.com。
    final static  String  endpoint = "oss-cn-beijing.aliyuncs.com";
    // 阿里云账号AccessKey拥有所有API的访问权限，风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维，请登录RAM控制台创建RAM用户。
    //LTAI5tJvkQMBfdpsuVuQwWcL
    //JXrMa0B5r7EO7V8fHpLbeDqpuhIZ2q
    final static String accessKeyId = "LTAI5tQzsjw6HaY1dzTmmjss";
    final static String accessKeySecret = "jAXhNqCr7p9FWKgYscBTbgwW515kdi";
    final static String bucketName = "mypackage0927";
    *//**oss
     * 阿里云文件上传
     * @param file
     * @return
     * @throws IOException
     *//*
    @PostMapping("/uploadimg")
    @ResponseBody
    public Result upload3(MultipartFile file) throws IOException {

// 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        String originalFilename = file.getOriginalFilename(); // 1.jpg 1.0.jpg
        String extension = originalFilename.substring(originalFilename.lastIndexOf("."));
        String newname = System.currentTimeMillis()+extension;
        System.out.println(newname);
// 创建PutObjectRequest对象。
// 依次填写Bucket名称（例如examplebucket）、Object完整路径（例如exampledir/exampleobject.txt）和本地文件的完整路径。Object完整路径中不能包含Bucket名称。
// 如果未指定本地路径，则默认从示例程序所属项目对应本地路径中上传文件。
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, newname,file.getInputStream() );

// 如果需要上传时设置存储类型和访问权限，请参考以下示例代码。
// ObjectMetadata metadata = new ObjectMetadata();
// metadata.setHeader(OSSHeaders.OSS_STORAGE_CLASS, StorageClass.Standard.toString());
// metadata.setObjectAcl(CannedAccessControlList.Private);
// putObjectRequest.setMetadata(metadata);

// 上传文件。
        ossClient.putObject(putObjectRequest);
        String path="https://"+bucketName+"."+endpoint+"/"+newname;
        Map map = new HashMap();
        map.put("path",path);
        map.put("filename",newname);
        System.out.println(map);
// 关闭OSSClient。
        ossClient.shutdown();
        return new Result(map);
    }
    *//**
     * 文件的下载
     * @param filename
     * @return
     *//*
    @GetMapping("/download")
    public ResponseEntity<byte[]> download(String filename, HttpServletRequest request) throws IOException {
        String realPath = request.getServletContext().getRealPath("/upload");
        File file = new File(realPath+File.separator+filename);
        HttpHeaders headers = new HttpHeaders();//http头信息
        // 文件的字符编码格式
        String downloadFileName = new String(filename.getBytes("iso-8859-1"),"utf-8");//设置编码
        headers.setContentDispositionFormData("attachment", downloadFileName);
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        //MediaType:互联网媒介类型  contentType：具体请求中的媒体类型信息
        return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file),headers, HttpStatus.CREATED);
        //return null;
    }*/
}

